package com.guragura.game.model;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.guragura.screens.GameScreen;

public class MovingObstacle extends Obstacle{
	private static final float FALLING_OFFSET_X = 46;
	private static final float FALLING_OFFSET_Y = 92;
	private static final float FALLING_SPEED_Y = 4;
	private static final float FALLING_SPEED_X = 2;
	
	private Boolean jatuh;
	private float drawX, drawY;
	private GameScreen screen;
	
	public MovingObstacle(Rectangle bound, GameScreen screen) {
		super(bound, screen,true);
		this.screen = screen;
		drawX = x - FALLING_OFFSET_X;
		drawY = y + FALLING_OFFSET_Y;
		jatuh = false;
	}
	
	public Boolean isJatuh(){
		return jatuh;
	}
	
	@Override
	public void draw(SpriteBatch batch){		
		if(screen.camera.position.y+Gdx.graphics.getHeight()/2>y+50){
		    stateTime += Gdx.graphics.getDeltaTime(); 
		    curFrame = animation.getKeyFrame(stateTime, false);
		    drawY-=FALLING_SPEED_Y;		    
		    drawX+=FALLING_SPEED_X;
		    if(!jatuh && (drawY-y<2)){
		    	jatuh = true;
		    	stateTime = 0;
		    	System.out.println();
		    }
		    if(jatuh){
		    	batch.draw(curFrame, x, y);
		    } else {
		    	batch.draw(animation.getKeyFrame(0.1f,true), drawX, drawY);
		    	batch.draw(animation.getKeyFrame(0.1f,true), drawX, drawY+5);
		    	batch.draw(animation.getKeyFrame(0.1f,true), drawX, drawY+10);
		    	batch.draw(animation.getKeyFrame(0.1f,true), drawX, drawY+15);
		    }
		}
	}
	

	public Boolean isCollide(GameObject o) {
		if(jatuh){
			updateRect();
			o.updateRect();
			return rect.overlaps(o.getRect());
		} else {
			return false;
		}
	}
}
